package com.kandinfo.isbnmanage.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.kandinfo.base.common.service.AbstractService;
import com.kandinfo.isbnmanage.domian.HomeworkTemporary;
import com.kandinfo.isbnmanage.repository.HomeworkTemporaryRepository;
import com.kandinfo.isbnmanage.service.HomeworkTemporaryService;



@Service
public class HomeworkTemporaryServiceImpl extends AbstractService<HomeworkTemporary, Long, HomeworkTemporaryRepository> implements HomeworkTemporaryService {
	
	@Resource
    public void setHomeworkTemporaryRepository(HomeworkTemporaryRepository homeworkTemporaryRepository){
        super.setRepository(homeworkTemporaryRepository);
    }

	@Override
	public List<HomeworkTemporary> findPagesByInforId(String barcode, Long isbnInformationId) {
		if (barcode != null && isbnInformationId != null) {
			return repository.find(barcode, isbnInformationId, "1");
		} else {
			return repository.findByIsbnInformationId(isbnInformationId, "1");
		}
	}
	

	@Override
	public Map<Long, List<HomeworkTemporary>> findPagesByBarcode(String barcode) {
		Map<Long, List<HomeworkTemporary>> map = new HashMap<Long, List<HomeworkTemporary>>();
		List<HomeworkTemporary> list = repository.findByBarcode(barcode);
		for (HomeworkTemporary h : list) {
			if (map.get(h.getHomeworkId()) == null) {
				map.put(h.getHomeworkId(), new ArrayList<HomeworkTemporary>());
			}
			map.get(h.getHomeworkId()).add(h);
		}
		return map;
	}
	
	

}
